package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.mjdb.data.field.Point3DField;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public  class DataColor extends Point3DField implements IPrimative{
	public static String DEFAULT_ID="_Color";

	public int alpha = 255;

	private TagModifier _modifier = TagModifier.POINT3D_TYPE_COLOR;

public DataColor() {
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch

	setRange(0,0,0,255,255,255);
}
public DataColor(int red, int green, int blue) {
	super(red, green, blue);

	try {
		setId(DEFAULT_ID, this);
	} catch (LockException le) {
	} //Dummy catch

	setRange(0, 0, 0, 255, 255, 255);
}
public DataColor(TagModifier type,int red, int green, int blue) {
	super(red, green, blue);

	_modifier = type;

	try {
		setId(_modifier.DEFAULT_ID, this);
	} catch (LockException le) {
	} //Dummy catch

	setRange(0, 0, 0, 255, 255, 255);
}
public void buildFromgrfxML(XmlElement xml)
{
	String attribName;
	String attribValue;

	for(int loop=0;loop<xml.getAttributeCount();loop++)
	{
		attribName = xml.getAttributeNameByIndex(loop);
		attribValue = xml.getAttributeValueByIndex(loop);

		if(attribName.equals(Token.ID))
		{
			if(attribValue!=null)
			{
				try{
					setId(attribValue,this);
				}
				catch(LockException le){} //Dummy catch
			}
		}
		else if(attribName.equals(_modifier.getAttributeAlias(Token.X)))
		{
			try{
				setValue(Token.X,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
		else if(attribName.equals(_modifier.getAttributeAlias(Token.Y)))
		{
			try{
				setValue(Token.Y,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
		else if(attribName.equals(_modifier.getAttributeAlias(Token.Z)))
		{
			try{
				setValue(Token.Z,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
		else if(attribName.equals("alpha"))
		{
			try{
				alpha = Integer.parseInt(attribValue);
			}
			catch(NumberFormatException ne){}
		}
		else if( !NameHelper.isSystemName(attribName) )
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_ATTRIB_CODE ,new Object[]{attribName,Token.COLOR,""+xml.getStartLineNo()} );
		}
	}
}
public void buildFromgrfxML(XmlElement xml,TagModifier type)
{
	_modifier = type;
	buildFromgrfxML(xml);
}
public void copyFromgrfxML(XmlElement xml)
{
	String attribName;
	String attribValue;

	for(int loop=0;loop<xml.getAttributeCount();loop++)
	{
		attribName = xml.getAttributeNameByIndex(loop);
		attribValue = xml.getAttributeValueByIndex(loop);

		if(attribName.equals(Token.ID))
		{
			// ignore
		}
		else if(attribName.equals(_modifier.getAttributeAlias(Token.X)))
		{
			try{
				setValue(Token.X,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
		else if(attribName.equals(_modifier.getAttributeAlias(Token.Y)))
		{
			try{
				setValue(Token.Y,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
		else if(attribName.equals(_modifier.getAttributeAlias(Token.Z)))
		{
			try{
				setValue(Token.Z,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
		else if(attribName.equals("alpha"))
		{
			try{
				alpha = Integer.parseInt(attribValue);
			}
			catch(NumberFormatException ne){}
		}
		else
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_ATTRIB_CODE ,new Object[]{attribName,Token.COLOR,""+xml.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml,TagModifier type)
{
	_modifier=type;
	copyFromgrfxML(xml);
}
public DataColor getClone() {

	DataColor newColor = new DataColor();

/*	for (int loop = 0; loop < _modifiers.attributes.length; loop++) {
		newColor.setAttributeName(attributes[loop].name, attributes[loop].value);
	}*/

	try{
		newColor.setValueXMin( getValueXMin() );
		newColor.setValueX( getValueX(),this );
		newColor.setValueXMax( getValueXMax() );

		newColor.setValueYMin( getValueYMin() );
		newColor.setValueY( getValueY(),this );
		newColor.setValueYMax( getValueYMax() );

		newColor.setValueZMin( getValueZMin() );
		newColor.setValueZ( getValueZ(),this );
		newColor.setValueZMax( getValueZMax() );

		newColor.alpha = alpha;
	}catch(LockException le){}

/*	try {
		XmlElement xml = new XmlParser().parseElement(togrfxML(DEFAULT_ID));
		newColor.buildFromgrfxML(xml);
	} catch (Exception e) {
		e.printStackTrace();
	}*/

	return newColor;
}
public DataColor getClone(String path) {

	DataColor newColor = getClone();

	GrfxMLStoreProcessor.setCloneName( path,this,newColor );

/*	for (int loop = 0; loop < attributes.length; loop++) {
		newColor.setAttributeName(attributes[loop].name, attributes[loop].value);
	}
	try {
		XmlElement xml = new XmlParser().parseElement(togrfxML(DEFAULT_ID));
		newColor.buildFromgrfxML(xml);
	} catch (Exception e) {
		e.printStackTrace();
	}*/

	return newColor;
}
public TagModifier getModifier() {
	return _modifier;
}
public void register(DataLinker store) {
	super.register(store);
}
public void replace(Field oldObj, Field newObj) {
}
public void replace(Field oldObj, Field newObj,boolean recursive) {
}
public void resetChanged() {
	super.resetChanged();
}
public String togrfxML(String name) {
	return IPrimativeHelper.togrfxML(this,name);
}
public String togrfxML(String name,TagModifier modifier) {
	return IPrimativeHelper.togrfxML(this,name,modifier);
}

public String togrfxML(IPrimative defaultField,String name) {
	return IPrimativeHelper.togrfxML(defaultField,this,name,null);
}
public String togrfxML(IPrimative defaultField,String name,TagModifier modifier) {
	return IPrimativeHelper.togrfxML(defaultField,this,name,modifier);
}

public String toString() {
	return IPrimativeHelper.toString(this);
}

public String getValue(String name) {
	if(name.equals(Token.T))
	{
		return ""+alpha;
	}
	else
	{
		return super.getValue(name);
	}
}

public int getValueT() {
	return alpha;
}

public void setValueT(int newAlpha,Object source) {
	alpha = newAlpha;
}
}
